<?php
namespace app\article\controller;
use think\Request;
use app\common\controller\AdminBase;

class ApiAdminArticlePush extends AdminBase
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->itemType = 'article';
        $this->itemName = '内容';
        $this->item = 'Articles';
        $this->itemCategory = 'ArticlesCategory';
        $this->itemContent = 'ArticlesContent';
        $this->itemSearch = 'ArticlesSearch';
        $this->itemSearchRel = 'ArticlesSearchRel';
        $this->itemTagsRel = 'ArticlesTagsRel';
        $this->itemModelNameSpace = 'app\article\model\Articles';
        $this->itemCategoryModelNameSpace = 'app\article\model\ArticlesCategory';
        $this->itemTagsRelModelNameSpace = 'app\article\model\ArticlesTagsRel';
    }
    
    public function urlPost(Request $request)
    {
        $api = input('post.api');
        if (!$api) {
            return jsonError('请先去设置推送的接口');
        }
        $url = input('post.url');
        if (!$url) {
            return jsonError('没有检测到你推送的页面地址');
        }
        $pushType = input('post.pushType');
        if (!$pushType) {
            return jsonError('没有检测到推送的类型');
        }
        $id = input('post.id');
        if (!$id) {
            return jsonError('没有检测到推送的ID');
        }
        $urls = explode(',',$url);
        if (is_array($urls)) {
            $result = pushData($api,$urls);
            $res = json_decode($result,true);
            if (!isset($res['error'])) {
                if ($pushType == 'baiduMip') {
                    db($this->item)->where('id',$id)->setInc('mip_push_num', 1);
                }
                if ($pushType == 'baiduTime') {
                    db($this->item)->where('id',$id)->setInc('link_push_num', 1);
                }
                if ($pushType == 'sm') {
                    db($this->item)->where('id',$id)->setInc('sm_push_num', 1);
                }
                if ($pushType == 'batch') {
                    db($this->item)->where('id',$id)->setInc('xzh_batch_push_num', 1);
                }
                if ($pushType == 'realtime') {
                    db($this->item)->where('id',$id)->setInc('xzh_realtime_push_num', 1);
                }
            }
            return jsonSuccess($result);
        } else {
            return jsonError('数据格式错误');
        }
    }

}